package de.fhaugsburg.games.boardgames.connect4.strategy;

import de.fhaugsburg.games.boardgames.connect4.Aintelligence;
import de.fhaugsburg.games.boardgames.connect4.BinaryPiece;
import de.fhaugsburg.games.boardgames.connect4.Connect4Board;

public class WeightedRobot implements Aintelligence {

	public int columnProposal(BinaryPiece piece, Connect4Board board) {
		
		
	int cpuMove = 0;
		
		//Board tempboard = getBoard().clone;
		
		// basics to know about the board
		int movesmade = board.getMovesMade();
		int cols = board.getWidth();
		@SuppressWarnings("unused")
		int rows = board.getHeight();
		
		
		if (movesmade <= 1) return cols / 2;	//first move always, center
		
		//offensive strategy
		int bestOffense = 0, bestDefense = 0;
		int moveValue;
		int offenseMove = (int) Math.round(Math.random() * cols);
		int defenseMove = (int) Math.round(Math.random() * cols);
		
		for (int i = 0; i < cols; i++) {
			if (board.checkColFree(i)){				//valid move?
				//offense
				moveValue = board.countEqualPiecesInARow(i, piece);
				if (moveValue > bestOffense){
					offenseMove = i;			//the best move found so far
					bestOffense = moveValue;
				}
				//defense
			/*	for (int j = 0; j < player.length; j++) {
					if (this != player[j]){		//enemy?
						moveValue = getBoard().countPiecesInARow(player[j], i);
						System.out.println(i + ": " + moveValue);
						if (moveValue > bestDefense){
							defenseMove = i;			//the best move found so far (enemy) -> disturb it
							bestDefense = moveValue;
						}
					}
				}*/
			}
		}
		if (bestDefense < bestOffense){
			cpuMove = offenseMove;
		}else{
			cpuMove = defenseMove;
		}		

		//int cpuMove = (int) Math.round(Math.random() * cols);			//random move for testing
		
		return cpuMove;		
	}

}
